import axios from 'axios';
import {
	Message,
	MessageBox
} from 'element-ui'
import Router from '@/router'
// 基本配置
const instance = axios.create({
	baseURL: 'http://43.198.233.28/', // 根据实际情况修改API地址
	timeout: 100000 // 设置超时时间，单位为ms
});

// 请求拦截器
instance.interceptors.request.use(config => {
	config.headers['token'] = localStorage.getItem('token'); // 设置请求头部分，这里举例使用了localStorage存储的token作为身份标识
	config.headers['lang'] = localStorage.getItem('languageTow'); // 设置请求头部分，这里举例使用了localStorage存储的token作为身份标识
	return config;
}, error => {
	console.log(error);
	return Promise.reject(error);
});

// 响应拦截器
instance.interceptors.response.use(response => {
	const data = response.data;
	if (data && data.status.errorCode == 401) { // 根据接口返回的状态码判断是否有错误
		// Message({
		//  message: '请先登录!',
		//  type: 'error',
		//  duration: 1.5 * 1000
		// })// 自定义错误提示
		// setTimeout(function(){
		localStorage.removeItem('token');
		if (localStorage.getItem('language')) {
			localStorage.removeItem('language');
		}
		localStorage.removeItem('siteTitle');
		localStorage.removeItem('siteIco');
		if (localStorage.getItem('languageTow')) {
			localStorage.removeItem('languageTow');
		}
		if (localStorage.getItem('userInfo')) {
			localStorage.removeItem('userInfo');
		}
		return Promise.resolve(data);
		// window.location.reload()

		// Router.push('/')
		// },500)
		// return Promise.reject(new Error(data.message));
	} else {
		return data;
	}
}, error => {
	console.log(error);
	return Promise.reject(error);
});

export default instance;